<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MContests
 *
 * @author andrea
 */
class MContests_vidwiz extends Model_vidwiz{
    
    public function __construct( ) {
        global $config;
        $this->tableName = $config['tables']['contests'];
        $this->installationFields = array(
                  array(
                      0 => 'id',
                      1 => 'int',
                      2 => 'NOT NULL AUTO_INCREMENT PRIMARY KEY'
                  ),
                  array(
                      0 => 'startDate',
                      1 => 'DATETIME',
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => 'stopDate',
                      1 => 'DATETIME',
                      2 => ''
                  ),
                  array(
                      0 => 'contestType',
                      1 => "ENUM ('open','private')",
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => "winnerSelection",
                      1 => "INT",
                      2 => "NOT NULL"
                  ),
                  array(
                      0 => 'description',
                      1 => 'TEXT',
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => "title",
                      1 => "TEXT",
                      2 => "NOT NULL"
                  ),
                  array(
                      0 => "ratingDuration",
                      1 => "INT",
                      2 => "NOT NULL"
                  ),
                  array(
                      0 => "uploadDuration",
                      1 => "INT",
                      2 => "NOT NULL"
                  ),
                  array(
                      0 => "status",
                      2 => "ENUM ('announced','subscription','user_validation','video_validation','ending_validation','voting','closed','mail_sent')",
                      3 => "NOT NULL"),
                  array(
                      0 => "page_id",
                      2 => "INT",
                      3 => "NOT NULL")  
                );
    }
    
    public function getContestFromId($id){
        global $config;
        $query = $this->searchQuery($config['tables']['contests'], array(array("id","=",$id)));
        $result = $this->getRowsAssoc($query);
        return $result[0];
    }

    public function allowVideoInContest($userid,$contestid,$allowdisallow){
        $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
        global $config;
        if($allowdisallow == "allow"){
            $valueSet = array(array("video_approved","1"));}
        else{
            $valueSet = array(array("video_approved","-1"));}
        $where = array(
            array("status","<>","announced"),
            array("status","<>","voting"),
            array("status","<>","closed")
            );
        $query2 = $this->searchQuery($config['tables']['contests'], $where, array("id"));
        $where = array(
            array("userid","=",$userid),
            array("user_approved","=","1"),
            array("contestid","=",$contestid),
            array("video_approved","=","0"),
            array("videoid","<>","0"),
            array("contestid"," IN ","(".$query2.")")
        );
        
        
        $query = $this->updateQuery($config['tables']['partecipations'], $valueSet, $where);
        
        $this->executeQuery($query);
    }
    
    
    public function getContestIdFromVideoInContest($videoid){
        $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
        global $config;
        $query = $this->searchQuery($config['tables']['partecipations'], array(array("videoid","=",$videoid)),array("contestid"));
        
        $result = $this->getRowsAssoc($query);
        return $result[0]['contestid'];
    }
    
    
}

?>
